package com.zhr.tea.view.activity;


import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.zhr.tea.R;
import com.zhr.tea.api.FenLeiApi;
import com.zhr.tea.entity.Pingfen;
import com.zhr.tea.entity.Pingjia;
import com.zhr.tea.entity.Product;
import com.zhr.tea.view.DividerItemDecoration;
import com.zhr.tea.view.adapter.PingjiaAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;
import okhttp3.Headers;

public class ProductActivity extends AppCompatActivity implements CBViewHolderCreator<ProductActivity.ConventHolder> {

    @BindView(R.id.product_back)
    ImageView productBack;
    @BindView(R.id.product_kefu)
    TextView productKefu;
    @BindView(R.id.product_dianpu)
    TextView productDianpu;
    @BindView(R.id.product_gouwuche)
    TextView productGouwuche;
    @BindView(R.id.product_add_gouwuche)
    TextView productAddGouwuche;
    @BindView(R.id.product_xiadan)
    TextView productXiadan;
    @BindView(R.id.product_btn_xiangqing)
    Button productBtnXiangqing;
    @BindView(R.id.product_btn_pingjia)
    Button productBtnPingjia;
    @BindView(R.id.product_recy)
    RecyclerView productRecy;
    @BindView(R.id.product_banner)
    ConvenientBanner<String> productBanner;
    @BindView(R.id.product_tv_title)
    TextView productTvTitle;
    @BindView(R.id.product_tv_guige)
    TextView productTvGuige;
    @BindView(R.id.product_tv_price)
    TextView productTvPrice;
    @BindView(R.id.product_tv_youji)
    TextView productTvYouji;
    @BindView(R.id.product_btn_add)
    Button productBtnAdd;
    @BindView(R.id.product_tv_count)
    TextView productTvCount;
    @BindView(R.id.product_btn_decress)
    Button productBtnDecress;
    @BindView(R.id.product_xiangqing)
    LinearLayout productXiangqing;
    @BindView(R.id.product_linerlayout)
    LinearLayout productLinerlayout;
    @BindView(R.id.activity_product)
    RelativeLayout activityProduct;


    private int goodId;

    private int count = 1;

    private PingjiaAdapter adapter;

    private List<Pingjia.ContentBean> pingjiaData = new ArrayList<>();

    private List<Pingfen.ContentBean> pingfenData = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);
        ButterKnife.bind(this);
        goodId = getIntent().getIntExtra("id", 33333);
        initView();
        initData();
        initPingjiaData();
    }


    private void initPingjiaData() {
        HttpRequest.get(FenLeiApi.PINGFEN_URL + String.valueOf(goodId), new BaseHttpRequestCallback<String>() {
            @Override
            protected void onSuccess(Headers headers, String s) {
                super.onSuccess(headers, s);
                String json = s.substring(s.indexOf("(") + 1, s.lastIndexOf(")"));
                Pingfen pingfen = JSON.parseObject(json, Pingfen.class);
                Pingfen.ContentBean content = pingfen.getContent();
                pingfenData.add(content);
                adapter.notifyDataSetChanged();
            }
        });
        HttpRequest.get(FenLeiApi.PINGJIA_URL + String.valueOf(goodId), new BaseHttpRequestCallback<String>() {
            @Override
            protected void onSuccess(Headers headers, String s) {
                super.onSuccess(headers, s);
                String json = s.substring(s.indexOf("(") + 1, s.lastIndexOf(")"));
                Pingjia pingjia = JSON.parseObject(json, Pingjia.class);
                List<Pingjia.ContentBean> conteneBean = pingjia.getContent();
                pingjiaData.addAll(conteneBean);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void initData() {
        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject json = new JSONObject();
        json.put("goodsId", goodId + "");
        params.applicationJson(json);

        HttpRequest.post(FenLeiApi.PRODUCT_URL, params, new BaseHttpRequestCallback<Product>() {
            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            protected void onSuccess(Product product) {
                super.onSuccess(product);
                Product.ContentBean content = product.getContent();
                productTvTitle.setText("【" + content.getGoodsName() + "】  " + content.getGoodsDes());
                productTvGuige.setText(content.getGoodsSpec() + "; " + content.getGoodsDynamicProperties());
                productTvPrice.setText("  ¥ " + content.getGoodsPrice());
                productTvYouji.setText("运费   免运费");
                productTvCount.setText(count + "");
                String[] arg = content.getGoodsImgUrls().split(",");
                List<String> urlData = new ArrayList<String>();
                for (int i = 0; i < arg.length; i++) {
                    urlData.add(arg[i]);
                }
                productBanner.setPages(ProductActivity.this, urlData);
            }
        });

    }

    private void initView() {
        adapter = new PingjiaAdapter(pingjiaData, pingfenData, this);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        productRecy.setLayoutManager(manager);
        productRecy.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));
        productRecy.setAdapter(adapter);
    }

    @OnClick({R.id.product_back, R.id.product_kefu, R.id.product_dianpu, R.id.product_gouwuche, R.id.product_add_gouwuche, R.id.product_xiadan, R.id.product_btn_xiangqing, R.id.product_btn_pingjia, R.id.product_btn_add, R.id.product_btn_decress})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.product_back:
                this.finish();
                break;
            case R.id.product_kefu:
                break;
            case R.id.product_dianpu:
                break;
            case R.id.product_gouwuche:
                break;
            case R.id.product_add_gouwuche:
                addToCar();
                break;
            case R.id.product_xiadan:
                break;
            case R.id.product_btn_xiangqing:
                productXiangqing.setVisibility(View.VISIBLE);
                productRecy.setVisibility(View.GONE);
                break;
            case R.id.product_btn_pingjia:
                productRecy.setVisibility(View.VISIBLE);
                productXiangqing.setVisibility(View.GONE);
                break;
            case R.id.product_btn_add:
                productTvCount.setText(String.valueOf(count + 1));
                count++;
                break;
            case R.id.product_btn_decress:
                if (count > 1) {
                    productTvCount.setText(String.valueOf(count - 1));
                    count--;
                }
                break;
        }
    }

    private void addToCar() {


    }

    @Override
    public ConventHolder createHolder() {
        return new ConventHolder();
    }


    class ConventHolder implements Holder<String> {
        ImageView img;

        @Override
        public View createView(Context context) {
            img = new ImageView(context);
            return img;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            Glide.with(context).load(data
            ).into(img);
        }


    }

}
